import edu.rit.ds.registry.NotBoundException;
import edu.rit.ds.registry.RegistryProxy;
import java.io.Serializable;
import java.rmi.RemoteException;

/**
 * @author Alan Kaminsky
 * @source P2Pedia example
 * 
 * @contributor Praneeth Pulusani
 * A sender sends a message that will be received by a certain receiver.
 * The receiver is identified by its numerical ID. The message itself is
 * just a text string. The sender actually gives the message (sender ID, 
 * receiver ID, and text) to an MQ server.
 * 
 * java Sender <host> <port> <servername> <senderid> <receiverid> "<message>"
 * <host> is the name of the host computer where the Registry Server is running.
 * <port> is the port number to which the Registry Server is listening.
 * <servername> is the MQ server's name.
 * <senderid> is the sender's ID, which must be an integer.
 * <receiverid> is the receiver's ID, which must be an integer.
 * "<message>" is the text of the message. The text must be enclosed in 
 * quotation marks in case the text contains spaces.
 */
public class Sender implements Serializable {

	public final int senderID;
	public final int receiverID;
	public final String message;

	/**
	 * Create a new sender object with its id, reciverd id and the message
	 * 
	 * @param senderID
	 *            Originating sender ID.
	 * @param receiverID
	 *            Where the message is going to
	 * @param message
	 *            message that is going to be transported
	 */
	public Sender(int senderID, int receiverID, String message) {
		this.senderID = senderID;
		this.receiverID = receiverID;
		this.message = message;
	}

	/**
	 * Sender main program. java Sender <host> <port> <servername> <senderid>
	 * <receiverid> "<message>"
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// Parse command line arguments.
		if (args.length != 6)
			usage();
		String host = args[0];
		int port = parseInt(args[1], "port");
		String servername = args[2];
		int senderID = parseInt(args[3], "senderid");
		int receiverID = parseInt(args[4], "receiverid");
		String message = args[5];

		
		RegistryProxy registry;
		MQServerRef mqserver;
		// Get a proxy for the Registry Server.
		try {
		registry = new RegistryProxy(host, port);
		} catch (RemoteException e) {
			throw new RemoteException(
					"There is no Registry server running at given host and port");
		}
		//check that message queuing server exists
		try {
		mqserver = (MQServerRef) registry.lookup(servername);
		} catch (NotBoundException exc) {
			throw new NotBoundException("MQServer with given name does not exist");
		} catch (RemoteException e) {
			throw e;
		}
		//send the message
		mqserver.sendMessage(senderID, receiverID, message);
		// Print results.
		// To <receiverid>: "<message>"
		System.out.printf("To %s: \"%s\"\n", receiverID, message);
	}

	/**
	 * Print a usage message and exit.
	 */
	private static void usage() {
		System.err.println("Usage: java Sender <host> <port> <servername> <senderid> <receiverid> \"<message>\"");
		System.exit(1);
	}

	/**
	 * Parse an integer command line argument.
	 * 
	 * @param arg
	 *            Command line argument.
	 * @param name
	 *            Argument name.
	 * 
	 * @return Integer value of <TT>arg</TT>.
	 * 
	 * @exception IllegalArgumentException
	 *                (unchecked exception) Thrown if <TT>arg</TT> cannot be
	 *                parsed as an integer.
	 */
	private static int parseInt(String arg, String name) {
		try {
			return Integer.parseInt(arg);
		} catch (NumberFormatException exc) {
			System.err.printf("Sender: Invalid <%s>: \"%s\"", name, arg);
			usage();
			return 0;
		}
	}
}
